<?php
$header_msg = "EEBS Main page";
include_once('header.inc');
require_once('db.php');

	if(!empty($_POST['register_login'])) { // this message may also fit in the header
		echo "<P>user created and logged in successfully</P>";
	}
	echo "<P>
			Welcome to our cute litte blog system. It is a task in a school project and
			very much under develpment. We hope it will make sense in time.
		</P><P>
			@bomann && zilter: i have now added a start for using the nicEdit.
			it should be straightforward implementing it, but my brain is
			right around exploding now. i have written the worklog for
			thursday. i will leave the rest of the task with you, i think it
			will be fine
		</P><P>
			<A HREF=index.php?most14=read>show most read entries last 14 days</A>
			<A HREF=index.php?most14=commented>show most commented entries last 14 days</A>
		</P>";
	//*/
	if (empty($_SESSION['USER_ID'])) {
		$sql ="SELECT * FROM `blogentries` ORDER BY -`created_date`";
	} if (!empty($_SESSION['USERGRP'])) {
		if ($_SESSION['USERGRP'] == 1) {
			$sql ="SELECT * FROM `blogentries` WHERE `user_id`=".$_SESSION['USER_ID']." ORDER BY -`created_date`";
		} else if ($_SESSION['USERGRP'] == 2) {
			$sql ="SELECT * FROM `blogentries` WHERE `vote_downs`<0 ORDER BY -`vote_downs`";
		}
	} if (!empty($_GET['most14'])) {
		if ($_GET['most14'] == 'read') {
			$sql ="SELECT * FROM `blogentries` WHERE `created_date`>'".(time()-1209600)."' ORDER BY -`times_read`";
		} else if ($_GET['most14'] == 'commented') {
			$sql ="SELECT * FROM `blogentries` WHERE `created_date`>'".(time()-1209600)."' ORDER BY -`num_comments`";
		}
	}
//	$lol = time()-1209600;
//	echo "<P>".$lol."</P>";
	$blogdbstatement = $eebsdb->query($sql)
		or die("<P>DB query for blogentries unsuccessfull. usually this is because
		the blog-post you are trying to read does not exist, or there are no
		blog-posts posted yet.</P>");
//		echo "<P>ladida before for loop</P>";
/*	if ($blogdbstatement->rowCount() < 1) {
		$blogdbstatement = $eebsdb->query("SELECT * FROM `blogentries` ORDER BY -`created_date`")
		or die("<P>DB query for blogentries unsuccessfull. usually this is because
		the blog-post you are trying to read does not exist, or there are no
		blog-posts posted yet.</P>");
	} //*/
	for ($blgvar = 0; $blgvar < $blogdbstatement->rowCount(); ++$blgvar) {
		$currpost = $blogdbstatement->fetch();
//		print_r($currpost);
		$sql = "SELECT * FROM `users` WHERE
			`id`='".$currpost['user_id']."'";
		$authstate = $eebsdb->query($sql) or die('could not find user in DB');
		$author = $authstate->fetch();
		?>
		<H2><A HREF="readblog.php?blognumber=<?PHP echo $currpost['id']?>">
			<?PHP echo $currpost['title']; ?></A></H2>
<!--		<DIV><?PHP //echo $currpost['entry'];?></DIV> -->
		<TABLE>
			<TR>
				<TD WIDTH="10%" ALIGN="left" COLSPAN="1">
					Author: <A HREF="mypage.php?ui=<?PHP echo $author['id']?>">
						<?PHP echo $author['username']; ?></A>
				</TD><TD WIDTH="15%" ALIGN="right" COLSPAN="1">
					<A HREF="readblog.php?blognumber=<?PHP echo $currpost['id']?>">
						comments:</A> <?PHP echo $currpost['num_comments']; ?>
				</TD><TD WIDTH="10%" ALIGN="right" COLSPAN="1">
					Date: <?PHP echo date("d M, Y H:i",$currpost['created_date']);?>
				</TD>
				<?php if (!empty($_SESSION['USER_ID'])) {
					if (($_SESSION['USERGRP'] == 2) ||
					($_SESSION['USERNAME'] == $author['username'])){
						?>
						<FORM METHOD=POST ACTION="editblogentry.php?blognumber=<?PHP echo $currpost['id']?>">
							<P><INPUT TYPE="SUBMIT" VALUE="edit post">
						</FORM>
						<?php
					}
				} ?>
			</TR>
		</TABLE>
		<?PHP
	} // for '$blgvar' end

	include_once('footer.inc');
?>
